package com.hellacutty.shake;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

public class CanvasActivity extends Activity {
    /** Called when the activity is first created. */
	private ShakeClass shake;
	private Handler handler = new Handler();
	private ImageView iv;
	private long start;
	private Runnable r = new Runnable(){
		@Override
		public void run() {
			// TODO Auto-generated method stub
			iv.invalidate();
		}

    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	start = System.currentTimeMillis();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.canvas);
        iv = (ImageView) findViewById(R.id.imageView1);
        iv.setImageDrawable(new Drawable(){
			private Bitmap android = BitmapFactory.decodeResource(getResources(),
		            R.drawable.android);
			public void draw(Canvas canvas) {
				// TODO Auto-generated method stub



                canvas.drawColor(Color.BLACK);
                canvas.drawBitmap(android, 100, shake.getShakeCount(), null);

                if(shake.getShakeCount() > iv.getHeight()-android.getHeight()){
                	long totalLength = System.currentTimeMillis()-start;
                	Log.d("finish time", ""+totalLength);

                }
                else{
                	handler.postDelayed(r, 16);
                }
			}

			@Override
			public int getOpacity() {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public void setAlpha(int alpha) {
				// TODO Auto-generated method stub
			}
			@Override
			public void setColorFilter(ColorFilter cf) {
				// TODO Auto-generated method stub

			}
        });
        shake = new ShakeClass((SensorManager)getSystemService(SENSOR_SERVICE));
    }
}